<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2017/3/16
 * Time: 15:40
 */

namespace Library;


class Reward
{
    public static function StartChouJiang($list){
        //生成数组
        //奖品列表
        $res = [];
        if(empty($list)){
            return false;
        }
        //获取奖品列表
        $array = [];
        foreach($list as $value){
            $res[$value->id] = [
                'name' => $value->name,
                'is_log' => $value->is_log,
                'tip_text' => $value->tip_text,
                'id' => $value->id,
                'is_endless' => $value->is_endless,
                'reward_cate' => $value->reward_cate
            ];
            if($value->current_number > 0){
                $array[$value->id] = $value->current_number;
            }
        }
        return $res[self::get_rand($array)];
    }


    public static function get_rand($array) {
        $result = '';
        //概率数组的总概率精度
        $sum = array_sum($array);
        //概率数组循环
        foreach ($array as $key => $number) {
            $randNum = mt_rand(1, $sum);
            if ($randNum <= $number) {
                $result = $key;
                break;
            } else {
                $sum -= $number;
            }
        }
        return $result;
    }
}